<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { guild_roomApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '100px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    title:'',
    dialogFormVisible:false,
    disabled:false,
  })
  interface formQuert {
    frame_id:number
    frame_name:string
    frame_img:string
    frame_desc:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    frame_name: [{ required: true, message: "请输入装饰框名称", trigger: "blur" }],
    frame_img: [{ required: true, message: "请上传装饰框图片", trigger: "blur" }],
    frame_desc: [{ required: true, message: "请输入装饰框描述", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      let submitSet = false
      const { frame_id } = form
      const loading = Toastloading()
      if(!frame_id) {
        await guild_roomApi.roomFrameAdd(form).then(()=>{
          submitSet = true
          ToastSuccess('新增成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await guild_roomApi.roomFrameEdit(form).then(()=>{
          submitSet = true
          ToastSuccess('编辑成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = async(query:any = {})=> {
    const { 
      id:frame_id=0, frame_name='',
      frame_img='', frame_desc='',
    } = query
    let formobj = {
      frame_name, frame_img, frame_desc,
    } as any
    if(frame_id)formobj['frame_id'] = frame_id
    Object.assign(form,formobj)
    action.title = frame_id?`编辑房间装饰框：${frame_name}`:'新增房间装饰框'
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="装饰框名称" prop="frame_name">
          <el-input v-model="form.frame_name" placeholder="请输入装饰框名称"  />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="装饰框图片" prop="frame_img">
          <xmUpload
            v-model:file-url="form.frame_img"
            :data="{
              md5:1,
              type:29,
            }"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="装饰框描述" prop="frame_desc">
          <el-input
            v-model="form.frame_desc"
            :autosize="{ minRows: 6, maxRows: 6}"
            type="textarea"
            placeholder="请输入装饰框描述"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>